﻿/*
COPYRIGHT (C) 2009 EPISERVER AB

THIS FILE IS PART OF VIRTUAL ROLES SAMPLE PACK FOR EPISERVER CMS.
DYNAMIC CONTENT CONTRIB FOR EPISERVER CMS IS FREE SOFTWARE: YOU CAN REDISTRIBUTE IT AND/OR MODIFY IT UNDER THE TERMS OF THE GNU LESSER GENERAL PUBLIC LICENSE VERSION v2.1 AS PUBLISHED BY THE FREE SOFTWARE FOUNDATION.

VIRTUAL ROLES SAMPLE PACK FOR EPISERVER CMS IS DISTRIBUTED IN THE HOPE THAT IT WILL BE USEFUL, BUT WITHOUT ANY WARRANTY; WITHOUT EVEN THE IMPLIED WARRANTY OF MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE. SEE THE GNU LESSER GENERAL PUBLIC LICENSE FOR MORE DETAILS.
YOU SHOULD HAVE RECEIVED A COPY OF THE GNU LESSER GENERAL PUBLIC LICENSE ALONG WITH VIRTUAL ROLES SAMPLE PACK FOR EPISERVER CMS. IF NOT, SEE <HTTP://WWW.GNU.ORG/LICENSES/>.
*/
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using EPiServer.Security;
using System.Web;

namespace EPiServer.Research.VirtualRoles
{
    /// <summary>
    /// Will filter a user based on the remote request. Optional parameters:
    /// "ip": if set, the remote IP should contain this string
    /// "host": if set, the remote host should contain this string
    /// "referer": if set, the referring url should contain this string
    /// "agent": if set, the user agent should contain this string
    /// </summary>
    public class RemoteFilterRole : VirtualRoleProviderBase
    {
        public string IP { get; set; }
        public string Host { get; set; }
        public string Referer { get; set; }
        public string Agent { get; set; }

        public override void Initialize(string name, System.Collections.Specialized.NameValueCollection config)
        {
            base.Initialize(name, config);
            IP = config["ip"];
            Host = config["host"];
            if (!string.IsNullOrEmpty(Host)) Host = Host.ToLower();
            Referer = config["referer"];
            if (!string.IsNullOrEmpty(Referer)) Referer = Referer.ToLower();
            Agent = config["agent"];
            if (!string.IsNullOrEmpty(Agent)) Agent = Agent.ToLower();
        }

        public override bool IsInVirtualRole(System.Security.Principal.IPrincipal principal, object context)
        {
            if (HttpContext.Current == null) return false;
            HttpContext con = HttpContext.Current;
            if (!string.IsNullOrEmpty(IP))
            {
                if (!con.Request.UserHostAddress.Contains(IP)) return false;
            }
            if (!string.IsNullOrEmpty(Host))
            {
                if (!con.Request.UserHostName.ToLower().Contains(Host)) return false;
            }
            if (!string.IsNullOrEmpty(Referer))
            {
                if (con.Request.UrlReferrer == null) return false;
                if (!con.Request.UrlReferrer.ToString().ToLower().Contains(Referer)) return false;
            }
            if (!string.IsNullOrEmpty(Agent))
            {
                if (!con.Request.UserAgent.ToLower().Contains(Agent)) return false;
            }
            return true;
        }
    }
}
